package com.by.yz.welcome;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.activity.LoginActivity;
import com.by.yz.activity.MainActivity;
import com.by.yz.common.ExitAppUtils;
import com.by.yz.common.MFGT;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.LoginRESTFul;

import org.json.JSONObject;

public class FirstWelcomeActivity extends AppCompatActivity {

    private static final int GO_LOGIN = 100;
    private static final int GO_GUIDE = 200;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("ywx","进入页面");
        requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题栏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,WindowManager.LayoutParams. FLAG_FULLSCREEN);//全屏
        setContentView(R.layout.first_welcome);
        ExitAppUtils.getInstance().addActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //判断是否第一次使用App
        if (App.getCacheToken(FirstWelcomeActivity.this) != null) {
            //不是第一次使用
            mHandler.sendEmptyMessageDelayed(GO_LOGIN, 2000);
        } else {
            //第一次使用
            mHandler.sendEmptyMessageDelayed(GO_GUIDE, 2000);
        }
    }

    private Handler  mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                final Intent intent = new Intent();
                switch (msg.what) {
                    case GO_LOGIN:
                        if (App.getCacheUserToken(FirstWelcomeActivity.this) != null) {
                            AccountRESTFul accountRequste = new AccountRESTFul();
                            accountRequste.get(new AccountRESTFul.IhttpCb() {
                                @Override
                                public void ok(JSONObject response) {
                                    String[] data = App.getCacheUser(FirstWelcomeActivity.this);
                                    Log.e("ywx","信息"+data[0]+data[1]);
                                    LoginRESTFul loginRESTFul = new LoginRESTFul();
                                    loginRESTFul.login(data[1], data[0], new LoginRESTFul.ILoginCb() {
                                        @Override
                                        public void ok(String response) {
                                            intent.setClass(FirstWelcomeActivity.this, MainActivity.class);
                                            startActivity(intent);
                                            MFGT.finish(FirstWelcomeActivity.this);
                                        }
                                        @Override
                                        public void error(String error) {
                                            Log.e("asd", error);
                                        }
                                    });
                                }

                                @Override
                                public void error(String error) { }
                            });
                        } else {
                            intent.setClass(FirstWelcomeActivity.this, LoginActivity.class);
                            startActivity(intent);
                            MFGT.finish(FirstWelcomeActivity.this);
                        }

                        break;
                    case GO_GUIDE:
                        //跳转GuideActivity
                        intent.setClass(FirstWelcomeActivity.this, WelcomeActivity.class);
                        startActivity(intent);
                        MFGT.finish(FirstWelcomeActivity.this);
                        break;
                }
//                Handler handler = new Handler();
//                handler.postDelayed(new Runnable() {
//                    public void run() {
//                        MFGT.finish(FirstWelcomeActivity.this);
//                    }
//                },2000);
            }
    };

}